 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1.Resources;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using PhoneApp1.Models;
using System.Collections.ObjectModel;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private async void Button_Click_DisplayBMIDetails(object sender, RoutedEventArgs e)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(serviceURI);         // base URL for API Controller i.e. RESTFul service

                    // add an Accept header for JSON
                    client.DefaultRequestHeaders.
                        Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    
                        // GET URL/api/AppBMI/parent name
                        // get all children list asynchronously - await the result (i.e. block and return control to caller)
                   

                        HttpResponseMessage response = await client.GetAsync(parentName.Text);      


                        // continue
                        if (response.IsSuccessStatusCode)                                                   // 200.299
                        {
                            
                            // read result and display on UI
                            var children = await response.Content.ReadAsAsync<IEnumerable<Class5to7>>();
                            
                                // set the data source for the children long list which will display in the MainPage.xaml children's name, BMI and category .
                               ChildrenLongList.ItemsSource = new List< Class5to7 > (children);
                        }
                        else
                        {
                            parentName.Text = " ";
                            MessageBox.Show("Please enter your name again");
                           
                        }
                }
            }
            catch (Exception )
                {
                    parentName.Text = " ";
                    MessageBox.Show("Please enter your name again");
                }

        }     //end of Button_Click_DisplayChildrenBMI
   
    }
}

      